package com.shockweb.common.calculate.function;

import java.math.BigDecimal;

import com.shockweb.common.calculate.CalculateException;
import com.shockweb.common.calculate.api.IFunction;

import java.util.List;

/**
 * 返回两个值中较大的一个
 * @author pengminghua
 *
 */
public class Max implements IFunction{

	/**
	 * @see IFunction#eval(List)
	 */
	public BigDecimal eval(List<Object> params) throws CalculateException {
		try{
			BigDecimal a = convert(params.get(0));
			BigDecimal b = convert(params.get(1));
			if(a.compareTo(b)==-1){
				return b;
			}else{
				return a;
			}
		}catch(Exception e){
			throw new CalculateException(e);
		}
	}
	
	/**
	 * 数据转换
	 * @param param
	 * @return
	 */
	private BigDecimal convert(Object param){
		BigDecimal value = null;
		if(param instanceof BigDecimal){
			value = (BigDecimal)param;
		}else{
			value = new BigDecimal((String)param);
		}
		return  value;
	}

	/**
	 * @see IFunction#getParamCount()
	 */
	public int getParamCount() {
		return 2;
	}
	
	/**
	 * @see IFunction#getName()
	 */
	public String getName() {
		return "max";
	}
}
